import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.aliyun.odps.udf.UDF;

public class CalculateShape extends UDF {

	
	
	
	
	/*
	 * public static void main(String[] args) { String title =
	 * "蓝佳美臻品肌因精华液（臻品肌因肽+臻品肌因精华）"; int categoryId = 147; evaluate(title,
	 * categoryId); }
	 */
	 
	 
	 
	public String evaluate(String title, Integer categoryId) {
		try {
			// 用于将匹配到的关键字替换掉
			String replaceTitle = title;
			// 将形态分类放在集合中存储
			Map<String, String> shapeCateMap = new LinkedHashMap<String, String>();
			shapeCateMap.put("盐", "浴盐,盐");
			shapeCateMap.put("液", "精华水,精华液,精华露,走珠液,走珠露,粉底液,液体皂,液体眼影,液体腮红,凝露,蜜露,纯露,液体,液态,皂液,香氛,唇彩,唇釉,唇采,颊彩,水,液,剂,露");
			shapeCateMap.put("油", "精华油,精油,油");
			shapeCateMap.put("霜", "精华霜,乳霜,凝霜,面霜,发膜,霜");
			shapeCateMap.put("乳", "精华乳,精华素,洗发水,洗头水,洗发精,洗面奶,洗发沐浴,洗沐,浴宝,发宝,乳液,乳剂,乳,素");
			shapeCateMap.put("精华","肌底液,原生液,原液,原浆,菁华,精粹,精华");
			shapeCateMap.put("固形物","块,饼,锭,皂");
			shapeCateMap.put("粉", "腮红,眼影,眼彩,胭脂,膜粉,粉剂,粉饼,粉条,散粉,蜜粉,粉");
			shapeCateMap.put("走珠", "走珠,滑珠,滚珠");
			shapeCateMap.put("膏", "啫喱膏,润唇宝,润唇球,口红,腮红膏,膏");
			shapeCateMap.put("泥", "泥浆面膜,泥膜,泥");
			shapeCateMap.put("蜡", "蜜蜡,蜡");
			shapeCateMap.put("胶", "指甲油,走珠啫喱,啫喱,啫哩,凝胶,彩胶,冰晶,指彩,甲彩,蜜,胶");
			shapeCateMap.put("泡", "慕斯,摩丝,慕丝,香波,泡沫,泡");
			shapeCateMap.put("笔", "马克笔,啫喱笔,胶笔,水笔,笔,棒,棍");
			//shapeCateMap.put("盒", "组合,盒,盘,包,套");
			shapeCateMap.put("巾", "肤巾,湿巾,棉,巾");
			shapeCateMap.put("气", "气雾剂,喷雾,喷剂,气雾,雾");
			shapeCateMap.put("膜", "膜贴,冰膜,膜,膜,贴");
			shapeCateMap.put("条", "条");

			// 创建map的作用是为了保存索引，并且去重，不用单列集合去重原因是因为单列的集合只会保存最先保存的数据，而这里是需要后面数据覆盖前面的。
			Map<Integer, String> indexMap = new HashMap<Integer, String>();
			// 保存关键字出现在title的索引位置和对应的形态名称
			Map<String, Integer> indexShapeMap = new HashMap<String, Integer>();
			// 保存索引的list
			List<Integer> indexList = new ArrayList<Integer>();
			// 保存结果的map
			Map<String, Object> resultMap = new HashMap<String, Object>();

			// 首先我们先根据商品分类来判断形态
			if (null != categoryId && categoryId != 0) {
				// 获取分类的id和对应的形态名称
				Map<Integer, String> categoryMap = categoryMap();
				String shapeNames = categoryMap.get(categoryId);
				// 判断分类不为空，并且形态名称只有一种 直接返回形态
				if (StringUtils.isNotBlank(shapeNames) && !shapeNames.contains(",")) {
					System.out.println("分类id不为空 只有一种分类 ：：：：" + shapeNames);
					resultMap.put("shape", shapeNames);
					resultMap.put("shapeSize", 1);
					System.out.println(JSON.toJSONString(resultMap));
					return JSON.toJSONString(resultMap);
				} else if (StringUtils.isNotBlank(shapeNames) && shapeNames.contains(",")) {
					//// 判断分类不为空，并且形态名称有多种
					String[] shapeNamesArr = shapeNames.split(",");
					for (String shape : shapeNamesArr) {
						// 根据形态取出对应的关键字进行匹配
						String shapeKeyWords = shapeCateMap.get(shape);
						String[] shapeKeyWordsArr = shapeKeyWords.split(",");
						for (String keyWord : shapeKeyWordsArr) {
							if (replaceTitle.contains(keyWord)) {
								// 包含
								// 将出现的形态名称放入map，出现多个相同的自动去重取最后一个
								// 用原始的title获取关键字出现的索引位置
								int index = title.lastIndexOf(keyWord);
								// 将已经出现的关键字替换掉，不在匹配其他的关键字
								replaceTitle = replaceTitle.replace(keyWord, "");
								System.out.println(replaceTitle);
								indexMap.put(index, shape);
								indexShapeMap.put(shape, index);
								// resultSet.add(shapeName);
								System.out.println("分类id不为空，并且有多种分类 -----" + "title :" + title + "---keyWord:" + keyWord
										+ "---index:" + index + "----shape:" + shape);
							}
						}
					}

				} else {
					// 这种情况就是商品本身有分类id，但是我存的集合当中没有他的分类（因为有的商品的分类无法判断形态，所以未加入到集合中），也只能全局匹配
					// 迭代集合中的形态分类
					Iterator<Entry<String, String>> iterator = shapeCateMap.entrySet().iterator();
					while (iterator.hasNext()) {
						Entry<String, String> shapeCate = iterator.next();
						// 获取形态的名称和关键字
						String shapeName = shapeCate.getKey();
						String shapeKeyWord = shapeCate.getValue();
						// System.out.println(shapeKeyWord);
						// 将关键字进行切割循环，看看是否title中是否包含
						String[] shapeKeyArr = shapeKeyWord.split(",");
						for (String keyWord : shapeKeyArr) {
							if (replaceTitle.contains(keyWord)) {
								// 包含
								// 将出现的形态名称放入map，出现多个相同的自动去重取最后一个
								// 用原始的title获取关键字出现的索引位置
								int index = title.lastIndexOf(keyWord);
								// 将已经出现的关键字替换掉，不在匹配其他的关键字
								replaceTitle = replaceTitle.replace(keyWord, "");
								System.out.println(replaceTitle);
								indexMap.put(index, shapeName);
								indexShapeMap.put(shapeName, index);
								// resultSet.add(shapeName);
								System.out.println("传过来的商品分类id不为空，但是我们存的分类中没有此分类"+"title :" + title + "---keyWord:" + keyWord + "---index:" + index
										+ "----shape:" + shapeName);
							}
						}

					}

				}

			} else {
				// 没有商品分类id的 只能全局匹配，将所有的形态分类进行匹配
				// 迭代集合中的形态分类
				Iterator<Entry<String, String>> iterator = shapeCateMap.entrySet().iterator();
				while (iterator.hasNext()) {
					Entry<String, String> shapeCate = iterator.next();
					// 获取形态的名称和关键字
					String shapeName = shapeCate.getKey();
					String shapeKeyWord = shapeCate.getValue();
					// System.out.println(shapeKeyWord);
					// 将关键字进行切割循环，看看是否title中是否包含
					String[] shapeKeyArr = shapeKeyWord.split(",");
					for (String keyWord : shapeKeyArr) {
						if (replaceTitle.contains(keyWord)) {
							// 包含
							// 将出现的形态名称放入map，出现多个相同的自动去重取最后一个
							// 用原始的title获取关键字出现的索引位置
							int index = title.lastIndexOf(keyWord);
							// 将已经出现的关键字替换掉，不在匹配其他的关键字
							replaceTitle = replaceTitle.replace(keyWord, "");
							indexMap.put(index, shapeName);
							indexShapeMap.put(shapeName, index);
							// resultSet.add(shapeName);
							System.out.println("没有商品分类id"+"title :" + title + "---keyWord:" + keyWord + "---index:" + index
									+ "----shape:" + shapeName);
						}
					}

				}
			}

			// 将去重的map迭代，把索引存到list 进行排序
			Iterator<Integer> iterator3 = indexMap.keySet().iterator();
			while (iterator3.hasNext()) {
				Integer index = iterator3.next();
				indexList.add(index);
			}
			// 集合遍历，按照索引位置先后排序获取对应的形态名称放入list
			System.out.println(indexShapeMap.size());
			StringBuffer sb = new StringBuffer();
			if (indexShapeMap.size() > 0) {
				Collections.sort(indexList);
				for (Integer i : indexList) {
					Iterator<String> iterator2 = indexShapeMap.keySet().iterator();
					while (iterator2.hasNext()) {
						String shapeName = iterator2.next();
						Integer index = indexShapeMap.get(shapeName);
						if (i == index) {
							// 按照索引顺序将形态名称拼接
							sb.append(shapeName).append(",");
						}
					}
				}
			}
			// 判断索引集合长度是否为0
			String shapeNames = sb.toString();
			if (shapeNames.contains(",")) {
				shapeNames = shapeNames.substring(0, shapeNames.lastIndexOf(","));
				// String shapeName = indexShapeMap.get(indexList.get(indexList.size() - 1));
				resultMap.put("shape", shapeNames);
				resultMap.put("shapeSize", indexShapeMap.size());
				System.out.println(JSON.toJSONString(resultMap));
				return JSON.toJSONString(resultMap);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private Map<Integer, String> categoryMap() {
		// 将整理好的分类存入map集合
		Map<Integer, String> cateMap = new HashMap<Integer, String>();
		cateMap.put(23, "精华,液");	    
		cateMap.put(25, "气");	    
		cateMap.put(29, "乳,精华,液");	    
		cateMap.put(31, "胶");	    
		cateMap.put(51, "膏,固形物");	    
		cateMap.put(53, "液");	    
		cateMap.put(96, "固形物");	    
		cateMap.put(101, "胶");		
		cateMap.put(102, "粉");		
		cateMap.put(103, "膏,霜,泥");		
		cateMap.put(104, "乳");		
		cateMap.put(105, "泡");		
		cateMap.put(106, "液");		
		cateMap.put(107, "油");		
		cateMap.put(108, "气");		
		cateMap.put(113, "油");		
		cateMap.put(114, "液");		
		cateMap.put(115, "膏");		
		cateMap.put(117, "胶");		
		cateMap.put(118, "霜");		
		cateMap.put(119, "泡");		
		cateMap.put(120, "油,精华");		
		cateMap.put(121, "膜");		
		cateMap.put(122, "气");		
		cateMap.put(125, "胶");		
		cateMap.put(126, "油");		
		cateMap.put(127, "膏");		
		cateMap.put(128, "乳");		
		cateMap.put(129, "精华");		
		cateMap.put(130, "霜");		
		cateMap.put(131, "液");		
		cateMap.put(133, "膏");		
		cateMap.put(134, "胶");		
		cateMap.put(136, "膜");		
		cateMap.put(137, "液");		
		cateMap.put(140, "液");		
		cateMap.put(141, "霜");		
		cateMap.put(142, "霜");		
		cateMap.put(143, "霜");		
		cateMap.put(145, "粉,液,霜,膜,精华");		
		cateMap.put(146, "油,膜,胶");		
		cateMap.put(147, "液,膜,粉,精华");		
		cateMap.put(148, "霜,乳");		
		cateMap.put(151, "霜,精华,液");		
		cateMap.put(153, "液");		
		cateMap.put(155, "膜,乳,油");		
		cateMap.put(156, "油");		
		cateMap.put(157, "固形物");		
		cateMap.put(159, "膜");		
		cateMap.put(160, "泥,膜");		
		cateMap.put(161, "粉,膜");		
		cateMap.put(163, "膜");		
		cateMap.put(164, "膜");		
		cateMap.put(166, "胶");		
		cateMap.put(168, "膜");		
		cateMap.put(169, "膜");		
		cateMap.put(170, "膏");		
		cateMap.put(171, "胶");		
		cateMap.put(175, "精华");		
		cateMap.put(177, "胶");		
		cateMap.put(179, "膜");		
		cateMap.put(182, "胶,霜,乳,液,精华");		
		cateMap.put(184, "霜");		
		cateMap.put(185, "膜");		
		cateMap.put(189, "笔");		
		cateMap.put(190, "乳,液");		
		cateMap.put(191, "霜");		
		cateMap.put(192, "气");		
		cateMap.put(197, "乳,霜");		
		cateMap.put(198, "霜,乳");		
		cateMap.put(202, "霜");		
		cateMap.put(203, "乳");		
		cateMap.put(204, "液");		
		cateMap.put(206, "霜");		
		cateMap.put(207, "霜");		
		cateMap.put(208, "液");		
		cateMap.put(209, "霜,棒,膏");		
		cateMap.put(210, "粉");		
		cateMap.put(211, "粉");		
		cateMap.put(214, "笔");		
		cateMap.put(215, "胶,液");		
		cateMap.put(216, "膏,霜");		
		cateMap.put(219, "粉");		
		cateMap.put(220, "笔");		
		cateMap.put(221, "霜");		
		cateMap.put(224, "笔");		
		cateMap.put(226, "粉");		
		cateMap.put(227, "膏,粉");		
		cateMap.put(230, "笔");		
		cateMap.put(231, "液");		
		cateMap.put(235, "固形物,液");		
		cateMap.put(236, "笔");		
		cateMap.put(240, "膏");		
		cateMap.put(241, "液");		
		cateMap.put(242, "精华");		
		cateMap.put(245, "膏");		
		cateMap.put(246, "液");		
		cateMap.put(247, "笔");		
		cateMap.put(249, "乳");		
		cateMap.put(250, "油");		
		cateMap.put(251, "精华");		
		cateMap.put(252, "乳");		
		cateMap.put(254, "乳,膏,盐,粉");		
		cateMap.put(258, "油");		
		cateMap.put(259, "膏,乳,固形物,笔,液");		
		cateMap.put(262, "霜,液");		
		cateMap.put(263, "膜");		
		cateMap.put(270, "膜,液");		
		cateMap.put(271, "霜");		
		cateMap.put(275, "霜");		
		cateMap.put(276, "膜");		
		cateMap.put(280, "霜,乳");		
		cateMap.put(281, "膜");		
		cateMap.put(282, "盐,粉,液");		
		cateMap.put(286, "胶,液");		
		cateMap.put(288, "胶");		
		cateMap.put(289, "乳,霜,膏");		
		cateMap.put(290, "精华");		
		cateMap.put(293, "液");		
		cateMap.put(296, "膏");		
		cateMap.put(298, "蜡");		
		cateMap.put(299, "膏,胶,泡");		
		cateMap.put(300, "乳,液");		
		cateMap.put(301, "笔,粉");		
		cateMap.put(302, "乳,液");		
		cateMap.put(303, "胶,乳,液,走珠");		
		cateMap.put(304, "气");		
		cateMap.put(307, "乳");		
		cateMap.put(308, "乳");		
		cateMap.put(309, "乳");		
		cateMap.put(311, "气");		
		cateMap.put(313, "固形物,膏");		
		cateMap.put(315, "霜");		
		cateMap.put(316, "油");		
		cateMap.put(317, "霜,乳,液,气,油,精华");		
		cateMap.put(318, "胶,霜");		
		cateMap.put(321, "泡,胶,膏,霜");		
		cateMap.put(322, "液,气");		
		cateMap.put(323, "乳,胶,霜,泥");		
		cateMap.put(324, "乳");		
		cateMap.put(326, "膏,液");		
		cateMap.put(328, "粉");		
		cateMap.put(364, "液");		
		cateMap.put(365, "霜");		
		cateMap.put(367, "乳,液");		
		cateMap.put(370, "乳");		
		cateMap.put(371, "液");		
		cateMap.put(373, "粉");		
		cateMap.put(374, "粉");		
		cateMap.put(375, "乳");		
		cateMap.put(378, "液");		
		cateMap.put(379, "固形物");		

		return cateMap;

	}
}
